(function (angular) {
    'use strict';

    angular.module('ytong.services').factory('ordersManager', ordersManager);

    function ordersManager(proxyService, Constants, growl, $log, authenticationService){

        var orders = [];
        var dashboardOrders = [];
        var editOrder = undefined;

        function loadOrders(){
            var salesmanID = undefined;
            if(authenticationService.validateUserRole(Constants.ROLES.SALESMAN)){
                 salesmanID = authenticationService.getCurrentUserID();
            }

            proxyService.get(Constants.URLS.ORDERS_URL, {params: {salesman_id: salesmanID}})
                .then(function(responseData){
                    $log.info("Orders retrieved successfully.");
                    orders= responseData.orders;
                }, function(responseData){
                    if(responseData.query_result == Constants.QUERY_RESULTS.NO_RESULTS){
                        orders = [];
                        $log.info("No orders.");
                    }else {
                        $log.info("Failed retrieving orders from server: " + responseData.log_error);
                        growl.error("Failed retrieving orders");
                        orders = [];
                    }
                });
        };

        function getOrders(){
            return orders;
        };

        function getOrdereStatus(order){
            switch(order.status){
                case Constants.ORDER_STATUS.OPEN: return "Open";
                case Constants.ORDER_STATUS.CLOSED: return "Closed";
                case Constants.ORDER_STATUS.IN_PROGRESS: return "In Progress";
            }
        };

        function addOrder(orderData) {

            var order = {};
            order.order = orderData;
            order.order.salesman_id = authenticationService.getCurrentUserID();

            proxyService.addEntity(Constants.URLS.ORDERS_URL,order)
                .then(function(){
                    growl.success("Order added");
                    loadOrders();
                }, function(){
                    growl.error("Order adding failed");
                });
        };

        function updateOrder(orderData) {

            var order = {};
            order.order = orderData;

            proxyService.updateEntity(Constants.URLS.ORDERS_URL,order)
                .then(function(){
                    growl.success("Order updated");
                    loadOrders();
                }, function() {
                    growl.error("Order updating failed");
                });
        };

        function deleteOrder(orderData) {

            var order = {};
            order.id = orderData.id;

            proxyService.deleteEntity(Constants.URLS.ORDERS_URL,order)
                .then(function(){
                    growl.success("Order deleted");
                    loadOrders();
                }, function(){
                    growl.error("Order deleting failed");
                });
        };

        function loadDashboardOrders(){
            var salesmanID = undefined;
            if(authenticationService.validateUserRole(Constants.ROLES.SALESMAN)){
                salesmanID = authenticationService.getCurrentUserID();
            }

            proxyService.get(Constants.URLS.ORDERS_DASHBOARD_URL, {params: {salesman_id: salesmanID}})
                .then(function(responseData){
                    $log.info("Orders dashboard retrieved successfully.");
                    dashboardOrders = responseData.orders_dashboard;
                }, function(responseData){
                    if(responseData.query_result == Constants.QUERY_RESULTS.NO_RESULTS){
                        dashboardOrders = [];
                        $log.info("No orders.");
                    }else {
                        $log.info("Failed retrieving orders dashboard from server: " + responseData.log_error);
                        growl.error("Failed retrieving orders dashboard ");
                        dashboardOrders = [];
                    }
                });
        };

        function getDashboardOrders(){
            return dashboardOrders;
        };

        function setEditOrder(order){
            editOrder = order;
        };

        function getEditOrder(){
            return editOrder;
        };

        //interface
        return{
            loadOrders: loadOrders,
            getOrders: getOrders,
            getOrdereStatus: getOrdereStatus,
            addOrder: addOrder,
            updateOrder: updateOrder,
            deleteOrder: deleteOrder,
            loadDashboardOrders: loadDashboardOrders,
            getDashboardOrders: getDashboardOrders,
            setEditOrder: setEditOrder,
            getEditOrder: getEditOrder
        }
    };

}(angular));